home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / C005.ZIP / EXP.C < prev    next >
Text File  |  1990-01-19  |  2KB  |  69 lines

  1. /********************************************************************
  2.  * C Users Group (U.K) C Source Code Library File CUGLIB.005        *
  3.  * Inquiries to: M. Houston, 36 Whetstone Clo. Farquhar Rd.         *
  4.  * Edgbaston, Birmingham B15 2QN ENGLAND                *
  5.  ********************************************************************
  6.  * File name: exp.c
  7.  * Program name: library modules only
  8.  * Source of file: The Public Domain Software Library.
  9.  * Purpose: maths function
  10.  * Changes: <who what when & why major changes have been made>      
  11.  ********************************************************************/
  12.  
  13.  
  14. /***************************************************
  15.  *        Exponential Routine                      *
  16.  *                                                 *
  17.  *        IBM PC "C" Language Board                *
  18.  *        Tulsa, OK 1918-664-8737                  *
  19.  *        SYSOP Lynn Long                          *
  20.  ***************************************************/
  21.  
  22.  
  23. #include "math.h"
  24. #include "errno.h"
  25.  
  26. #define P0 0.249999999999999993e+0
  27. #define P1 0.694360001511792852e-2
  28. #define P2 0.165203300268279130e-4
  29. #define Q0 0.500000000000000000e+0
  30. #define Q1 0.555538666969001188e-1
  31. #define Q2 0.495862884905441294e-3
  32.  
  33. #define P(z) ((P2*z + P1)*z + P0)
  34. #define Q(z) ((Q2*z + Q1)*z + Q0)
  35.  
  36. #define EPS     2.710505e-20
  37.  
  38. double
  39. exp(x)
  40. double x;
  41. {
  42.         int n;
  43.         double xn, g, r, z;
  44.         extern int errno;
  45.  
  46.         if (x > LOGHUGE) {
  47.                 errno = ERANGE;
  48.                 return HUGE;
  49.         }
  50.         if (x < LOGTINY) {
  51.                 errno = ERANGE;
  52.                 return 0.0;
  53.         }
  54.         if (fabs(x) < EPS)
  55.                 return 1.0;
  56.         n = z = x * 1.4426950408889634074;
  57.         if (n < 0)
  58.                 --n;
  59.         if (z-n >= 0.5)
  60.                 ++n;
  61.         xn = n;
  62.         g = ((x - xn*0.693359375)) + xn*2.1219444005469058277e-4;
  63.         z = g*g;
  64.         r = P(z)*g;
  65.         r = 0.5 + r/(Q(z)-r);
  66.         return ldexp(r,n+1);
  67. }
  68.  
  69.